/***************************************************************************
 *   oboe.controllers.jml.helpers.JMLClipper                               *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/
package oboe.controllers.jml.helpers
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import oboe.controllers.jml.events.JMLEvent;
	import oboe.controllers.jml.helpers.scrollBar.Bar;
	import oboe.controllers.jml.helpers.scrollBar.HBar;
	import oboe.controllers.jml.helpers.scrollBar.VBar;
	
	public class JMLClipper extends Sprite
	{
		public var showHBar:Boolean = false;
		public var showVBar:Boolean = false;
		
		public function JMLClipper()
		{
			super();
			this.addEventListener( JMLEvent.JMLCOMPLETE, onComplete);
			Object( this ).addCallback( Event.ADDED_TO_STAGE, onAdd ).once = true;
		}
		
		private function onAdd():void
		{
			if( showHBar ) this.parent.addChild( hbar = new HBar( this ) );
			if( showVBar ) this.parent.addChild( vbar = new VBar( this ) );
		}
		
		private function onComplete( e:JMLEvent ):void
		{
			if( e.source.width > 0 && e.source.height > 0 )
			{
				var clip:Shape = new Shape();
				clip.graphics.beginFill(0xFFFFFF, 1);
				clip.graphics.drawRect(0, 0, e.source.width, e.source.height );
				this.addChild( clip );
				clip.visible = false;
				this.mask = clip;
				this.scaleX = this.scaleY = 1;
			}
			// this.cacheAsBitmap = true;
		}
		
		private var vbar:Bar;
		private var hbar:Bar;
		
		public function setScrollbarVisible( state:Boolean ):void
		{
			if( !state )
			{
				if( vbar && vbar.parent == this ) this.parent.removeChild( vbar );
				if( hbar && vbar.parent == this ) this.parent.removeChild( hbar );
			}
			else
			{
				if( vbar ) this.parent.addChild( vbar );
				if( hbar ) this.parent.addChild( hbar );
			}
		}
	}
}